﻿using FOModels.WorkingCapitalLoan;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataAccess.WorkingCapitalLoan
{
    public class WorkingCapitalLoanDAL
    {
        private SQLDataAccess db;
        public WorkingCapitalLoanDAL()
        {
            db = new SQLDataAccess();
        }
        public DataTable getTOP1()
        {
            return db.exeSelect("Select TOP 1 * from WorkingCapitalLoan order by WorkingCapitalLoanID desc");
        }
        public DataTable getall(int langid)
        {
            return db.exeSelect("Select * from WorkingCapitalLoan sa join WorkingCapitalLoanLang hl on sa.WorkingCapitalLoanID = hl.WorkingCapitalLoanID join Company co on sa.CompanyID = co.CompanyID join CompanyLang cl on co.CompanyID = cl.CompanyID join Language la on hl.LangID = la.ID  where co.Active = 1 and hl.LangID = " + langid + " and cl.LangID = " + langid + "");
        }
        public DataTable getWorkingCapitalLoanrelati(int CompanyID, int WorkingCapitalLoanID, int langid)
        {
            return db.exeSelect("select * from WorkingCapitalLoan ca join WorkingCapitalLoanLang hl on ca.WorkingCapitalLoanID = hl.WorkingCapitalLoanID join Company co on ca.CompanyID = co.CompanyID join CompanyLang cl on co.CompanyID = cl.CompanyID where co.Active = 1 and ca.CompanyID = " + CompanyID + " and ca.WorkingCapitalLoanID != " + WorkingCapitalLoanID + " and Display = 1 and cl.LangID = " + langid + " and hl.LangID = " + langid + "");
        }
        public DataTable getWorkingCapitalLoan(int planid, string balance, int years, int langid)
        {
            return db.exeSelect("Select * from WorkingCapitalLoan sa join WorkingCapitalLoanLang hl on sa.WorkingCapitalLoanID = hl.WorkingCapitalLoanID join Company co on sa.CompanyID = co.CompanyID join CompanyLang cl on co.CompanyID = cl.CompanyID join TableCapitalLoanInterest ta on co.CompanyID = ta.CompanyID where co.Active = 1 and sa.HomeLoanPlanID = " + planid + " and sa.MinimumBalance <= " + balance + " and sa.MaximumBalance >= " + balance + " and ta.Years = " + years + " and Display = 1 and hl.LangID= " + langid + " and cl.LangID = " + langid + "");
        }
        public DataTable getWorkingCapitalLoanByProvider(int planid, string balance, int years, int CompanyID, int langid)
        {
            return db.exeSelect("Select * from WorkingCapitalLoan sa join WorkingCapitalLoanLang hl on sa.WorkingCapitalLoanID = hl.WorkingCapitalLoanID join Company co on sa.CompanyID = co.CompanyID join CompanyLang cl on co.CompanyID = cl.CompanyID join TableCapitalLoanInterest ta on co.CompanyID = ta.CompanyID where co.Active = 1 and sa.HomeLoanPlanID = " + planid + " and sa.MinimumBalance <= " + balance + " and sa.MaximumBalance >= " + balance + " and ta.Years = " + years + " and Display = 1 and hl.LangID= " + langid + " and cl.LangID = " + langid + " and sa.CompanyID = " + CompanyID + "");
        }
        public DataTable getWorkingCapitalLoanDefault(int langid)
        {
            return db.exeSelect("Select * from WorkingCapitalLoan sa join WorkingCapitalLoanLang hl on sa.WorkingCapitalLoanID = hl.WorkingCapitalLoanID join Company co on sa.CompanyID = co.CompanyID join CompanyLang cl on co.CompanyID = cl.CompanyID join TableCapitalLoanInterest ta on co.CompanyID = ta.CompanyID where co.Active = 1 and sa.HomeLoanPlanID = 2 and sa.MinimumBalance <= 50000000 and sa.MaximumBalance >= 50000000 and ta.Years = 1 and Display = 1 and hl.LangID= " + langid + " and cl.LangID = " + langid + "");
        }
        public DataTable getWorkingCapitalLoanDefaultByProvider(int CompanyID, int langid)
        {
            return db.exeSelect("Select * from WorkingCapitalLoan sa join WorkingCapitalLoanLang hl on sa.WorkingCapitalLoanID = hl.WorkingCapitalLoanID join Company co on sa.CompanyID = co.CompanyID join CompanyLang cl on co.CompanyID = cl.CompanyID join TableCapitalLoanInterest ta on co.CompanyID = ta.CompanyID where co.Active = 1 and sa.HomeLoanPlanID = 2 and sa.MinimumBalance <= 50000000 and sa.MaximumBalance >= 50000000 and ta.Years = 1 and Display = 1 and hl.LangID= " + langid + " and cl.LangID = " + langid + " and sa.CompanyID=" + CompanyID + "");
        }
        public DataTable getWorkingCapitalLoanByID(int WorkingCapitalLoanID, int langid)
        {
            return db.exeSelect("select * from WorkingCapitalLoan ca join WorkingCapitalLoanLang hl on ca.WorkingCapitalLoanID = hl.WorkingCapitalLoanID join Company co on ca.CompanyID = co.CompanyID join CompanyLang cl on co.CompanyID = cl.CompanyID where co.Active = 1 and ca.WorkingCapitalLoanID = " + WorkingCapitalLoanID + " and cl.LangID = " + langid + " and hl.LangID = " + langid + "");
        }
        public DataTable getWorkingCapitalLoanByIDManager(int WorkingCapitalLoanID, int langid)
        {
            return db.exeSelect("select * from WorkingCapitalLoan ca join WorkingCapitalLoanLang hl on ca.WorkingCapitalLoanID = hl.WorkingCapitalLoanID join Company co on ca.CompanyID = co.CompanyID join CompanyLang cl on co.CompanyID = cl.CompanyID where ca.WorkingCapitalLoanID = " + WorkingCapitalLoanID + " and cl.LangID = " + langid + " and hl.LangID = " + langid + "");
        }
        public int UpdateWorkingCapitalLoan(WorkingCapitalLoanModels models)
        {
            return db.exeUpdate("update WorkingCapitalLoan set WorkingCapitalLoanName = N'" + models.WorkingCapitalLoanName + "', MinimumBalance=" + double.Parse(models.MinimumBalance) + ",MaximumBalance=" + double.Parse(models.MaximumBalance) + ",HomeLoanPlanID=" + models.HomeLoanPlanID + ",CompanyID= " + models.CompanyID + ",Display='" + models.Display + "' where WorkingCapitalLoanID = " + models.WorkingCapitalLoanID + "");
        }
        public int InsertWorkingCapitalLoan(WorkingCapitalLoanModels models)
        {
            return db.exeUpdate("insert into WorkingCapitalLoan values (N'" + models.WorkingCapitalLoanName + "'," + models.CompanyID + "," + double.Parse(models.MinimumBalance) + "," + double.Parse(models.MaximumBalance) + "," + models.HomeLoanPlanID + ",'" + models.Display + "')");
        }
        public int DeleteWorkingCapitalLoan(int WorkingCapitalLoanID)
        {
            return db.exeUpdate("Delete from WorkingCapitalLoan where WorkingCapitalLoanID = " + WorkingCapitalLoanID + "");
        }
        public int UpdateWorkingCapitalLoanLang(WorkingCapitalLoanModels models, int langid)
        {
            return db.exeUpdate("update WorkingCapitalLoanLang set WorkingCapitalLoanName = N'" + models.WorkingCapitalLoanName + "', Benefit = N'" + models.Benefit + "',Description=N'" + models.Description + "',FeeContent=N'" + models.FeeContent + "',MoreInfo=N'" + models.MoreInfo + "' where WorkingCapitalLoanID = " + models.WorkingCapitalLoanID + " and LangID=" + langid + "");
        }
        public int InsertWorkingCapitalLoanLang(WorkingCapitalLoanModels models, int langid)
        {
            return db.exeUpdate("insert into WorkingCapitalLoanLang values (" + models.WorkingCapitalLoanID + "," + langid + ",N'" + models.WorkingCapitalLoanName + "',N'" + models.Benefit + "',N'" + models.Description + "',N'" + models.FeeContent + "',N'" + models.MoreInfo + "')");
        }

        public int DeleteWorkingCapitalLoanLang(int WorkingCapitalLoanID)
        {
            return db.exeUpdate("Delete from WorkingCapitalLoanLang where WorkingCapitalLoanID = " + WorkingCapitalLoanID + "");
        }

        public int DeleteWorkingCapitalLoanApply(int WorkingCapitalLoanApplyID)
        {
            return db.exeUpdate("Delete from WorkingCapitalLoanApply where WorkingCapitalLoanApplyID = " + WorkingCapitalLoanApplyID + "");
        }
        public DataTable getcontractbyid(int WorkingCapitalLoanapplyid)
        {
            return db.exeSelect("select * from WorkingCapitalLoanApply ca join [WorkingCapitalLoan] c on ca.WorkingCapitalLoanID = c.WorkingCapitalLoanID join [Status] ta on ca.StatusID = ta.StatusID join Company co on c.CompanyID = co.CompanyID join CompanyLang cl on co.CompanyID = cl.CompanyID where co.Active = 1 and ca.WorkingCapitalLoanApplyID = " + WorkingCapitalLoanapplyid + " and cl.LangID = 1");
        }
        public DataTable getcontract()
        {
            return db.exeSelect("select * from WorkingCapitalLoanApply ca join [WorkingCapitalLoan] c on ca.WorkingCapitalLoanID = c.WorkingCapitalLoanID join [Status] ta on ca.StatusID = ta.StatusID join Company co on c.CompanyID = co.CompanyID join CompanyLang cl on co.CompanyID = cl.CompanyID where co.Active = 1 and cl.LangID = 1");
        }
        public DataTable getcontractbystatuscount(int statusid)
        {
            return db.exeSelect("select * from WorkingCapitalLoanApply ca join [WorkingCapitalLoan] c on ca.WorkingCapitalLoanID = c.WorkingCapitalLoanID join [Status] ta on ca.StatusID = ta.StatusID join Company co on c.CompanyID = co.CompanyID join CompanyLang cl on co.CompanyID = cl.CompanyID where co.Active = 1 and ca.StatusID = " + statusid + " and cl.LangID = 1");
        }
        public int WorkingCapitalLoanApply(int WorkingCapitalLoanID, WorkingCapitalLoanApplyModels app)
        {
            return db.exeUpdate("insert into WorkingCapitalLoanApply(WorkingCapitalLoanID,CustomerName,CustomerEmail,CustomerPhone,CustomerIncome,CustomerDOB,StatusID) values (" + WorkingCapitalLoanID + ",N'" + app.CustomerName + "',N'" + app.CustomerEmail + "',N'" + app.CustomerPhone + "',N'" + app.CustomerIncome + "',N'" + app.CustomerDOB + "'," + app.StatusID + ")");
        }
        public int UpdateStatus(WorkingCapitalLoanApplyModels WorkingCapitalLoan)
        {
            return db.exeUpdate("update WorkingCapitalLoanApply set StatusID= " + WorkingCapitalLoan.StatusID + " where WorkingCapitalLoanApplyID = " + WorkingCapitalLoan.WorkingCapitalLoanApplyID + "");
        }
    }
}
